package com.ym.mq;

import com.aliyun.openservices.ons.api.PropertyKeyConst;
import com.aliyun.openservices.ons.api.bean.ProducerBean;
import com.ym.manager.admin.properties.Config;
import com.ym.utils.Common;

import java.util.Properties;

public class MqUtil {


    private static ProducerBean producerBean = null;

    /**
     * 获取消息的 Producer
     *
     * @return Producer
     */
    public static ProducerBean getProducer() {
        if(Common.isEmpty(producerBean)){
            Properties properties = new Properties();
            properties.put(PropertyKeyConst.AccessKey, Config.mq.AccessKey);
            properties.put(PropertyKeyConst.SecretKey, Config.mq.SecretKey);
            properties.put(PropertyKeyConst.NAMESRV_ADDR, Config.mq.NameSevAddr);
            ProducerBean producer = new ProducerBean();
            producer.setProperties(properties);

            // 在发送消息前，必须调用start方法来启动Producer，只需调用一次即可。
            producer.start();
            producerBean = producer;
        }
        return producerBean;
    }
}
